home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
14642
/
14642.xpi
/
chrome
/
modules
/
trackers
/
contact.js
< prev
next >
Wrap
Text File
|
2009-10-19
|
12KB
|
268 lines
/* Copyright 2009, Boomtango.com. All Rights Reserved. */
/* contact.js
* Responsible for tracking hContacts on pages
*/
var EXPORTED_SYMBOLS = ["contact"];
var contact = {
name: "Contact",
name_plural: "Contacts",
color: "#2F3A97",
isInternal: true,
_cache: null,
json: Components.classes["@mozilla.org/dom/json;1"].createInstance(Components.interfaces.nsIJSON),
onLoadTracks: function(url, doc){
var tracks = [];
this._cache = {};
if(doc){
var vcards = doc.getElementsByClassName('vcard');
var len = vcards.length;
for(var x = 0; x < len; x++){
var node = vcards[x];
var nnodes = node.getElementsByClassName('fn');
if(nnodes.length){
var name = nnodes[0].textContent;
var tnodes = node.getElementsByClassName('tel');
var anodes = node.getElementsByClassName('adr');
var tlen = tnodes.length;
var alen = anodes.length;
if(tlen || alen){
var info = { name: name, phone: [] };
if(alen){
var addr = anodes[0];
var list = addr.getElementsByClassName('locality');
if(list.length){
info['city'] = list[0].textContent;
list = addr.getElementsByClassName('street-address');
if(list.length){
info['address'] = list[0].textContent;
}
list = addr.getElementsByClassName('region');
if(list.length){
info['state'] = list[0].textContent;
}
list = addr.getElementsByClassName('postal-code');
if(list.length){
info['zip'] = list[0].textContent;
}
list = addr.getElementsByClassName('country-name');
if(list.length){
info['country'] = list[0].textContent;
}
// check for malformed google maps results
} else if(addr.id && addr.id.match(/^panel/)){
list = addr.getElementsByClassName('name');
if(list.length){
var namefields = list[0].getElementsByClassName('gcpaddrlink');
if(namefields.length){
name = namefields[0].textContent;
info.name = this.app.getString('contact.noname',name);
info.address = name;
}
}
list = addr.getElementsByClassName('atxt');
if(list.length){
info['city'] = list[0].textContent;
}
}
}
for(var y = 0; y < tlen; y++){
var telephone = tnodes[y].textContent;
if(telephone.length){
info.phone.push(telephone);
}
}
if(info.city || info.phone.length){
var preview = this.json.encode(info);
if(!this._cache[preview]){
tracks.push({
type: "contact",
title: info.name,
preview: preview,
content: name
}
);
this._cache[preview] = true;
}
}
}
}
}
}
return tracks;
},
onUnloadTracks: function(url, doc){
var tracks = [];
if(doc){
var vcards = doc.getElementsByClassName('vcard');
var len = vcards.length;
for(var x = 0; x < len; x++){
var node = vcards[x];
var nnodes = node.getElementsByClassName('fn');
if(nnodes.length){
var name = nnodes[0].textContent;
var tnodes = node.getElementsByClassName('tel');
var anodes = node.getElementsByClassName('adr');
var tlen = tnodes.length;
var alen = anodes.length;
if(tlen || alen){
var info = { name: name, phone: [] };
if(alen){
var addr = anodes[0];
var list = addr.getElementsByClassName('locality');
if(list.length){
info['city'] = list[0].textContent;
list = addr.getElementsByClassName('street-address');
if(list.length){
info['address'] = list[0].textContent;
}
list = addr.getElementsByClassName('region');
if(list.length){
info['state'] = list[0].textContent;
}
list = addr.getElementsByClassName('postal-code');
if(list.length){
info['zip'] = list[0].textContent;
}
list = addr.getElementsByClassName('country-name');
if(list.length){
info['country'] = list[0].textContent;
}
// check for malformed google maps results
} else if(addr.id && addr.id.match(/^panel/)){
list = addr.getElementsByClassName('name');
if(list.length){
var namefields = list[0].getElementsByClassName('gcpaddrlink');
if(namefields.length){
name = namefields[0].textContent;
info.name = this.app.getString('contact.noname',name);
info.address = name;
}
}
list = addr.getElementsByClassName('atxt');
if(list.length){
info['city'] = list[0].textContent;
}
}
}
for(var y = 0; y < tlen; y++){
var telephone = tnodes[y].textContent;
if(telephone.length){
info.phone.push(telephone);
}
}
if(info.city || info.phone.length){
var preview = this.json.encode(info);
if(!this._cache[preview]){
tracks.push({
type: "contact",
title: info.name,
preview: preview,
content: name
}
);
this._cache[preview] = true;
}
}
}
}
}
}
this._cache = {};
return tracks;
},
preview: function(doc, node, data){
var item = this.json.decode(data.preview);
var hbox, label, spacer;
// map
if(item.city){
var iframe = doc.createElement(
"iframe"
);
var addr = item.address || ' ';
var st = item.state || ' ';
var zip = item.zip || ' ';
var cty = item.country || ' ';
iframe.setAttribute("src",
"http://ext.boomtango.com/map.html?address=" +
encodeURIComponent(this.app.getString(
"contact.mapformat",addr,item.city,st, zip, cty)));
iframe.setAttribute("width", "352");
iframe.setAttribute("height", "296");
node.appendChild(iframe);
}
// name
hbox = doc.createElement('hbox');
hbox.setAttribute('style', 'padding-left: 8px;');
label = doc.createElement('label');
label.setAttribute('value', item.name);
label.setAttribute('flex', '1');
label.setAttribute('crop', 'end');
hbox.appendChild(label);
spacer = doc.createElement('spacer');
spacer.setAttribute('flex', '100');
hbox.appendChild(spacer);
node.appendChild(hbox);
// address
if(item.address){
hbox = doc.createElement('hbox');
hbox.setAttribute('style', 'padding-left: 8px;');
label = doc.createElement('label');
label.setAttribute('value', item.address);
label.setAttribute('flex', '1');
label.setAttribute('crop', 'end');
hbox.appendChild(label);
spacer = doc.createElement('spacer');
spacer.setAttribute('flex', '100');
hbox.appendChild(spacer);
node.appendChild(hbox);
}
// city, st, zip
if(item.city){
hbox = doc.createElement('hbox');
hbox.setAttribute('style', 'padding-left: 8px;');
label = doc.createElement('label');
var st = item.state || ' ';
var zip = item.zip || ' ';
label.setAttribute('value',
this.app.getString("contact.citystatezip",
item.city, st, zip));
label.setAttribute('flex', '1');
label.setAttribute('crop', 'end');
hbox.appendChild(label);
spacer = doc.createElement('spacer');
spacer.setAttribute('flex', '100');
hbox.appendChild(spacer);
node.appendChild(hbox);
}
// country
if(item.country){
hbox = doc.createElement('hbox');
label = doc.createElement('label');
label.setAttribute('value', item.country);
label.setAttribute('flex', '1');
label.setAttribute('crop', 'end');
hbox.appendChild(label);
spacer = doc.createElement('spacer');
spacer.setAttribute('flex', '100');
hbox.appendChild(spacer);
node.appendChild(hbox);
}
for(var x = 0; x < item.phone.length; x++){
hbox = doc.createElement('hbox');
label = doc.createElement('label');
label.setAttribute('value', item.phone[x]);
label.setAttribute('flex', '1');
label.setAttribute('crop', 'end');
hbox.appendChild(label);
spacer = doc.createElement('spacer');
spacer.setAttribute('flex', '100');
hbox.appendChild(spacer);
node.appendChild(hbox);
}
}
};